<?php
/**
 * Created by PhpStorm.
 * User: js
 * Date: 2018/9/26
 * Time: 17:52
 */

namespace app\modules\mch\models;

use app\models\Agent;
use app\models\Store;
use yii\data\Pagination;

class AgentListForm extends Model
{
    public $store_id;
    public $pingtai_id;

    public $page;
    public $limit;
    public $keyword;

    public function rules()
    {
        return [
            [['page'],'default','value'=>1],
            [['limit'],'default','value'=>20],
            [['keyword'],'trim'],
            [['keyword'],'string'],
        ];
    }

    public function search()
    {
        if(!$this->validate()){
            return $this->getModelError();
        }

        $query = Agent::find()->where(['pingtai_id'=>$this->pingtai_id,'is_delete'=>0]);

        if($this->keyword){
            $query->andWhere(['like','name',$this->keyword]);
        }

        $count = $query->count();
        $p = new Pagination(['totalCount'=>$count,'pageSize'=>$this->limit]);

        $list = $query->offset($p->offset)->limit($p->limit)->orderBy(['id'=>SORT_ASC])->asArray()->all();
        return [
            'list'=>$list,
            'p'=>$p,
            'row_count'=>$count
        ];
    }
    public function list_search()
    {
        if(!$this->validate()){
            return $this->getModelError();
        }

        $query = Agent::find()->alias('a')->leftjoin(['s' => store::tableName()], 'a.store_id=s.id')->where(['a.pingtai_id'=>1,'a.is_delete'=>0]);

        if($this->keyword){
            $query->andWhere(['like','s.name',$this->keyword]);
        }

        $count = $query->count();
        $p = new Pagination(['totalCount'=>$count,'pageSize'=>$this->limit]);

        $list = $query->offset($p->offset)->limit($p->limit)->orderBy(['a.id'=>SORT_ASC])->select('a.store_id,s.name')->asArray()->all();
        return [
            'list'=>$list,
            'p'=>$p,
            'row_count'=>$count
        ];
    }

}